<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/11 0011
 * Time: 21:36
 */
namespace app\common\excel;


class Export
{
    public function export($name, $arr)
    {
        $ex_arr = [];
        if ($arr) {
            // $arr['总金额'] = 0;

            foreach ($arr as &$value) {
                $ex_arr[] = [
                    'shop_order_number' => $value['shop_order_number'],
//                    'create_time'       => date('Y-m-d H:i:s', $value['create_time']),
                    'price'             => $value['price'],
                    'address_name'      => $value['address_name'],
                    'pay_time'          => date('Y-m-d H:i:s', $value['pay_time']),
                    'username'          => $value['username'],
                    'title'             => $value['title'],
                    'id'                => $value['id'],
                    'g_title'           => $value['g_title'],
                    'coupon_price'      => $value['coupon_price'],
                    'red_packet_price'  => $value['red_packet_price'],
                    'address_site'      => $value['address_site'],
                    'address_phone'     => $value['address_phone'],
                    'number'            => $value['number'],
                    'way'               => $value['way'],
                    'g_price'           => $value['g_price']
                ];
            }
        }

        //导出excel
        self::push($arr, $name);
    }


    public function coupon_export($name, $arr)
    {
        $ex_arr = [];
        if ($arr) {
            // $arr['总金额'] = 0;

            foreach ($arr as $value) {
                $ex_arr[] = [
                    '会员'            => $value['username'] . '/' . $value['name'],
                    '红包标题'          => $value['title'],
                    '优惠券实际金额（满减金额）' => $value['actual_price'] . '/' . $value['virtual_price'],
                    '使用状态'          => $value['status'],
                    '领取时间'          => date('Y-m-d', $value['create_time']),

                ];
            }
        }
        //导出excel
        self::push_coupon($ex_arr, $name);
    }

    public function push_coupon($data,$name = 'Excel')
    {
        set_time_limit(0);
        ini_set('memory_limit', '1024M');
        error_reporting(E_ALL);
        date_default_timezone_set('Asia/Shanghai');

        import('phpoffice.phpexcel.Classes.PHPExcel', EXTEND_PATH, '.php');
        $objPHPExcel = new \PHPExcel();

        /*以下是一些设置 ，什么作者  标题啊之类的*/
        $objPHPExcel->getProperties()->setCreator("管理员")
            ->setLastModifiedBy("管理员")
            ->setTitle("电话本导出")
            ->setSubject("数据EXCEL导出")
            ->setDescription("备份数据")
            ->setKeywords("excel")
            ->setCategory("result file");
        $objPHPExcel->setActiveSheetIndex(0)
            //Excel的第A列，shop_name是你查出数组的键值，下面以此类推
            ->setCellValue('A1', '会员')
            ->setCellValue('B1', '红包标题')
            ->setCellValue('C1', '优惠券实际金额（满减金额）')
            ->setCellValue('D1', '使用状态')
            ->setCellValue('E1', '领取时间');
        $sheet1 = ['A', 'B', 'C', 'D', 'E'];

        for ($x = 0; $x < count($sheet1); $x++) {
            $objPHPExcel->getActiveSheet()->getStyle($sheet1[$x] . '1')->applyFromArray(
                array(
                    'font'      => array(
                        'bold' => true
                    ),
                    'alignment' => array(
                        'horizontal' =>
                            \PHPExcel_Style_Alignment::HORIZONTAL_CENTER
                    )
                )
            );
        }

        /*以下就是对处理Excel里的数据， 横着取数据，主要是这一步，其他基本都不要改*/

        $n = 0;
        foreach ($data as $v) {

            $num = $n + 2;
            $objPHPExcel->setActiveSheetIndex(0)
                //Excel的第A列，shop_name是你查出数组的键值，下面以此类推
                ->setCellValue('A' . $num, $v['会员'])
                ->setCellValue('B' . $num, $v['红包标题'])
                ->setCellValue('C' . $num, $v['优惠券实际金额（满减金额）'])
                ->setCellValue('D' . $num, $v['使用状态'])
                ->setCellValue('E' . $num, $v['领取时间']);

            $n++;
        }

        $objPHPExcel->getActiveSheet(0)->setTitle('每月汇总表');
        $objPHPExcel->setActiveSheetIndex(0);

        ob_end_clean();
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;charset=utf-8;filename="' . $name . '.xlsx"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
        exit;
    }



    /* 导出excel函数*/
    public function push($data, $name = 'Excel')
    {
        set_time_limit(0);
        ini_set('memory_limit', '1024M');
        error_reporting(E_ALL);
        date_default_timezone_set('Asia/Shanghai');

        import('phpoffice.phpexcel.Classes.PHPExcel', EXTEND_PATH, '.php');
        $objPHPExcel = new \PHPExcel();

        /*以下是一些设置 ，什么作者  标题啊之类的*/
        $objPHPExcel->getProperties()->setCreator("管理员")
            ->setLastModifiedBy("管理员")
            ->setTitle("电话本导出")
            ->setSubject("数据EXCEL导出")
            ->setDescription("备份数据")
            ->setKeywords("excel")
            ->setCategory("result file");
        $objPHPExcel->setActiveSheetIndex(0)
            //Excel的第A列，shop_name是你查出数组的键值，下面以此类推
            ->setCellValue('A1', '订单编号')
            ->setCellValue('B1', '店铺名称')
            ->setCellValue('C1', '买家账号')
            ->setCellValue('D1', '下单时间')
            ->setCellValue('E1', '商品ID')
            ->setCellValue('F1', '商品名称')
            ->setCellValue('G1', '订单数量')
            ->setCellValue('H1', '订单总额')
            ->setCellValue('I1', '抵用金额')
            ->setCellValue('J1', '实付金额')
            ->setCellValue('K1', '抵用积分')
            ->setCellValue('L1', '支付方式')
            ->setCellValue('M1', '收货地址')
            ->setCellValue('N1', '收货人')
            ->setCellValue('O1', '收货人手机号')
            ->setCellValue('P1', '商品单价');
        $sheet1 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N','O','P'];

        for ($x = 0; $x < count($sheet1); $x++) {
            $objPHPExcel->getActiveSheet()->getStyle($sheet1[$x] . '1')->applyFromArray(
                array(
                    'font'      => array(
                        'bold' => true
                    ),
                    'alignment' => array(
                        'horizontal' =>
                            \PHPExcel_Style_Alignment::HORIZONTAL_CENTER
                    )
                )
            );
        }

        /*以下就是对处理Excel里的数据， 横着取数据，主要是这一步，其他基本都不要改*/

        $n = 0;
        foreach ($data as $v) {

            $num = $n + 2;
            $objPHPExcel->setActiveSheetIndex(0)
                //Excel的第A列，shop_name是你查出数组的键值，下面以此类推
                ->setCellValue('A' . $num, $v['shop_order_number'])
                ->setCellValue('B' . $num, '善食元')
                ->setCellValue('C' . $num, $v['username'])
                ->setCellValue('D' . $num, $v['create_time'])
                ->setCellValue('E' . $num, $v['id'])
                ->setCellValue('F' . $num, $v['g_title'])
                ->setCellValue('G' . $num, $v['number'])
                ->setCellValue('H' . $num, $v['price'])
                ->setCellValue('I' . $num, $v['use_balance'])
                ->setCellValue('J' . $num, $v['pay_price'])
                ->setCellValue('K' . $num, $v['use_integral'])
                ->setCellValue('L' . $num, $v['way'])
                ->setCellValue('M' . $num, $v['address_area'].'-'.$v['address_site'])
                ->setCellValue('N' . $num, $v['address_name'])
                ->setCellValue('O' . $num, $v['address_phone'])
                ->setCellValue('P' . $num, $v['g_price']);

            $n++;
        }

        $objPHPExcel->getActiveSheet(0)->setTitle('每月汇总表');
        $objPHPExcel->setActiveSheetIndex(0);

        ob_end_clean();
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;charset=utf-8;filename="' . $name . '.xlsx"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
        exit;

    }
}